<?php
/**
 * 交易记录
 * Created by PhpStorm.
 * User: andy
 * Date: 2019/12/31
 * Time: 上午9:42
 */
namespace app\common\model;

use think\Model;

class UserRecordModel extends Model
{
    const DEL_STATUS=-1; //删除
    const HIDE_STATUS=0; //无效
    const SHOW_STATUS=1; //收入
    const Z_STATUS=2; //支出

    protected $autoWriteTimestamp = true;

    public function user()
    {
        return $this->hasOne('MemberModel','uid','user_id')->field('uid,mobile,alipay_user,wechat_user');
    }

    /**
     * 所有推荐
     * @param array $where
     * @param int $pageSum
     * @return \think\Paginator
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getALL($where=[],$pageSum=0)
    {
        if($pageSum<=0){
            $pageSum=getSystem("pageSUM")?getSystem("pageSUM"):10;
        }

        $list=self::where('status','>',self::HIDE_STATUS)
            ->where($where)
            ->order('create_time DESC')
            ->paginate($pageSum);

        return $list;
    }


    /**
     * 删除
     * @param $id
     * @return array
     */
    public function del($id){

        $au=self::get($id);
        $au->status=self::DEL_STATUS;

        $status=$au->save();
        if($status){
            return ['status'=>1,'msg'=>'删除成功！'];
        }else{
            return ['status'=>0,'msg'=>'删除失败！'];
        }

    }

    /**
     * 增加
     * @param $data
     * @return bool
     */
    public static function adds($user_id,$status,$giveMoney,$nowMoney,$content='',$title){

        $now_balance=bcadd($nowMoney,$giveMoney,2);

        $data=[
            "user_id"=>$user_id,
            "title"=>$title,
            "status"=>$status,
            "money"=>$giveMoney,
            "now_balance"=>$now_balance,
            "content"=>$content,
            "create_time"=>time(),
        ];


        $status = self::insertGetId($data);

        if($status){
            return['status'=>1,'msg'=>'增加成功！'];
        }else{
            return['status'=>0,'msg'=>'增加失败！'];
        }

    }

} 